package com.cat.model.web.action.api;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.cat.model.biz.service.api.UserService;
import com.cat.model.common.model.common.UserTokenVO;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.dao.mapper.UserInfoMapper;
import com.cat.model.web.action.base.BaseAction;


public class UserAction extends BaseAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8684395903948270846L;

	private String token;
	
	
	private String nickName;
	private String phoneNo;
	private String oldPassword;
	private String password;
	private String realName;
	
	private File headImage; 
	private String headImageFileName;
		
	private String content;
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private UserService userService;
	
	/**
	 * 获取用户信息
	 * */
	public void info(){
		if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		print(userService.userInfo(userToken.getUserId()));
	}
	
	/**
	 * 修改用户昵称
	 * */
	public void editNickName(){
		if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		if(StringUtils.isEmpty(nickName)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		
		print(userService.updateNickName(userToken.getUserId(), nickName));
	}
	
	/**
	 * 修改用户真实姓名
	 * */
	public void editRealName(){
		if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		if(StringUtils.isEmpty(realName)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		
		print(userService.updateRealName(userToken.getUserId(), realName));
	}
	
	/**
	 * 修改用户头像
	 * */
	public void editHeadImage(){
		if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		if(null == headImage){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		print(userService.updateHeadImage(userToken.getUserId(), headImage, headImageFileName));
	}
	
	/**
	 * 修改用户密码
	 * */
	public void editPassword(){
		if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		if(StringUtils.isEmpty(password) || StringUtils.isEmpty(oldPassword)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		
		print(userService.updatePassword(userToken.getUserId(), oldPassword, password));
	}
	
	/**
	 * 用户反馈
	 * */
	public void suggestion(){
		if(StringUtils.isEmpty(token) || StringUtils.isBlank(content)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		print(userService.suggestion(userToken.getUserId(), content));
	}
	
	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getPhoneNo() {
		return phoneNo;
	}

	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public File getHeadImage() {
		return headImage;
	}

	public void setHeadImage(File headImage) {
		this.headImage = headImage;
	}

	public String getHeadImageFileName() {
		return headImageFileName;
	}

	public void setHeadImageFileName(String headImageFileName) {
		this.headImageFileName = headImageFileName;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
}
